package com.xmut.rc.controller;

import com.xmut.rc.common.utils.JwtUtil;
import com.xmut.rc.common.Result;
import com.xmut.rc.entity.SysPermission;
import com.xmut.rc.entity.vo.LoginVo;
import com.xmut.rc.entity.vo.RegisterVo;
import com.xmut.rc.entity.vo.SysUserVo;
import com.xmut.rc.service.SysPermissionService;
import com.xmut.rc.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description: 登录注册
 * @author: XS
 * @date: 2022/5/23
 */
@Api(tags = "登录注册")
@RestController
@RequestMapping("/system/rbac/")
@Slf4j
public class AccessController {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysPermissionService sysPermissionService;


    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping("/login")
    public Result<?> login(@RequestBody LoginVo loginVo) {
        HashMap<String, String> login = new HashMap<>();
        String token = sysUserService.login(loginVo);
        login.put("token", token);
        return Result.OK(login);
    }

    @ApiOperation(value = "登出", notes = "登出")
    @PostMapping("/logout")
    public Result<?> logout() {
        return Result.OK("success");
    }

    @ApiOperation(value = "登录用户信息", notes = "登录用户信息")
    @GetMapping("/getInfo")
    public Result<?> getInfo(@RequestParam("token") String token) {
        String userId = JwtUtil.getUserIdByJwtToken(token);
        SysUserVo loginUserInfo = sysUserService.getLoginUserInfo(userId);
        return Result.OK(loginUserInfo);
    }

    @ApiOperation(value = "登录用户权限", notes = "登录用户权限")
    @GetMapping("/getPermission")
    public Result<?> getPermission(@RequestParam("token") String token) {
        List<SysPermission> permission = sysPermissionService.getPermission(token);
        if (permission != null && !permission.isEmpty()) {
            return Result.OK(permission);
        }
        return Result.error("查询权限失败!");
    }

    @ApiOperation(value = "注册", notes = "注册")
    @PostMapping("register")
    public Result<?> register(@RequestBody RegisterVo registerVo) {
        sysUserService.register(registerVo);
        return Result.OK();
    }

    @ApiOperation(value = "生成验证码", notes = "生成验证码")
    @GetMapping("generateValidateCode")
    public Result<?> generateValidateCode(@RequestParam("validateCodeId") String validateCodeId) {
        String base64 = sysUserService.generateValidateCode(validateCodeId);
        return Result.OK(base64);
    }

    @ApiOperation(value = "发送邮箱验证码", notes = "发送邮箱验证码")
    @GetMapping("sendEmailValidateCode")
    public Result<?> sendEmailValidateCode(@RequestParam("validateCodeId") String validateCodeId, @RequestParam(name = "targetEmail") String targetEmail) {
        if (sysUserService.generateEmailValidateCode(validateCodeId, targetEmail)) {
            return Result.OK("邮箱验证码发送成功");
        } else {
            return Result.OK("邮箱验证码发送失败");
        }
    }

    @ApiOperation(value = "找回密码", notes = "找回密码")
    @GetMapping("findPassword")
    public Result<?> findPassword(@RequestParam("username") String username, @RequestParam("email") String email) {
        if (sysUserService.findPassword(username, email)) {
            return Result.OK("邮件已发送");
        } else {
            return Result.error("邮箱错误");
        }
    }

    @ApiOperation(value = "重置密码", notes = "重置密码")
    @GetMapping("resetPassword")
    public Result<?> resetPassword(@RequestParam("password") String password, @RequestParam("key") String key) {
        if (sysUserService.resetPassword(password, key)) {
            return Result.OK("密码重置成功");
        } else {
            return Result.error("链接已过期，请重新发送");
        }
    }

    @ApiOperation(value = "修改密码", notes = "修改密码")
    @GetMapping("modifyPassword")
    public Result<?> modifyPassword(@RequestParam String id, @RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword) {
        if (sysUserService.modifyPassword(id, oldPassword, newPassword)) {
            return Result.OK("密码修改成功");
        } else {
            return Result.error("密码修改失败");
        }
    }
}
